package com.atguigu.yygh.hosp.controller;

import com.atguigu.yygh.common.R;
import com.atguigu.yygh.common.handler.YyghException;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//医院设置接口
@Api(description = "医院设置接口")
@RestController//Controller返回重定向  RestController返回json数据
@RequestMapping("/admin/hosp/hospitalSet")
//@CrossOrigin //跨域
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;


    @ApiOperation(value = "模拟登录")
    @PostMapping("login")
    public R login(){
        //{"code":20000,"data":{"token":"admin-token"}}
        return R.ok().data("token","admin-token");
    }

    @ApiOperation(value = "用户信息")
    @GetMapping("info")
    public R info(){
        //{"code":20000,"data":{"roles":["admin"],
        // "introduction":"I am a super administrator",
        // "avatar":"https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif",
        // "name":"Super Admin"}}
        Map<String,Object> map = new HashMap<>();
        map.put("roles","admin");
        map.put("introduction","I am a super administrator");
        map.put("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        map.put("name","Super Admin");
        return R.ok().data(map);
    }






    //查询所有医院设置
    @ApiOperation(value = "医院设置列表")
    @GetMapping("findAll")
    public R findAll(){
       /* try {
            int i = 1/0;
        } catch (Exception e) {
            e.printStackTrace();
            throw  new YyghException(20001,"自定义异常处理");
        }*/
        List<HospitalSet> list = hospitalSetService.list();
        return R.ok().data("list",list);
    }
    //根据id删除医院
    @ApiOperation(value = "根据id删除医院")
    @DeleteMapping("{id}")
    public R removeById(@PathVariable Long id){
        boolean remove = hospitalSetService.removeById(id);
        return R.ok();
    }

    @ApiOperation(value = "分页医院设置列表")
    @GetMapping("{page}/{limit}")
    public R pageList( @PathVariable Long page,
                       @PathVariable Long limit){
        Page<HospitalSet> pageParam = new Page<>(page,limit);
        hospitalSetService.page(pageParam);
        List<HospitalSet> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return R.ok().data("records",records)
                .data("total",total);
    }

    @ApiOperation(value = "带条件分页医院设置列表")
    @PostMapping("pageQuery/{page}/{limit}")
    public R pageQuery(@PathVariable Long page,
                       @PathVariable Long limit,
                       @RequestBody HospitalSetQueryVo hospitalSetQueryVo){
        //1获取参数
        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();
        //2验空拼写查询条件
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(hosname)){
            wrapper.like("hosname",hosname);
        }
        if(!StringUtils.isEmpty(hoscode)){
            wrapper.eq("hoscode",hoscode);
        }
        //3创建分页对象进行查询
        Page<HospitalSet> pageParam = new Page<>(page,limit);
        hospitalSetService.page(pageParam,wrapper);
        List<HospitalSet> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return R.ok().data("records",records)
                .data("total",total);
    }

    @ApiOperation(value = "医院设置新增")
    @PostMapping("save")
    public R save(@RequestBody HospitalSet hospitalSet ){
        boolean save = hospitalSetService.save(hospitalSet);
        if(save){
            return R.ok();
        }else {
            return R.error();
        }
    }


    @ApiOperation(value = "根据id查询医院设置")
    @GetMapping("getHospSetById/{id}")
    public R getHospSetById(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return R.ok().data("hospitalSet",hospitalSet);
    }

    @ApiOperation(value = "医院设置修改")
    @PostMapping("update")
    public R update(@RequestBody HospitalSet hospitalSet ){
        boolean update = hospitalSetService.updateById(hospitalSet);
        if(update){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation(value = "根据id集合批量删除医院")
    @DeleteMapping("batchRemove")
    public R batchRemove(@RequestBody List<Long> ids){
        boolean remove = hospitalSetService.removeByIds(ids);
        if(remove){
            return R.ok();
        }else {
            return R.error();
        }
    }

    // 医院设置锁定和解锁
    @PutMapping("lockHospitalSet/{id}/{status}")
    public R lockHospitalSet(@PathVariable Long id,
                             @PathVariable Integer status) {
        //1先查询
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //2更新状态值
        hospitalSet.setStatus(status);
        boolean update = hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }

}
